package view;

import entity.Customer;
import entity.Order;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import services.Service;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Map;

public class Report1JPanel {
    JPanel backgroundPanel;
    Service service;
    public Report1JPanel(Service service, IndexJFrame indexJFrame) {
        this.service = service;
        backgroundPanel = new JPanel(new BorderLayout());
        initChartPanel();
    }

    private void initChartPanel() {
        Map<String, Order> orderMap = service.getOrderMap();
        Map<String, Customer> customerMap = service.getCustomerMap();
        //构建数据集合
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(Order order : orderMap.values()){
            dataset.addValue(order.getTotal(),(new SimpleDateFormat("yy-MM")).format(order.getTransactionDate()),
                    customerMap.get(order.getCustomerId()).getName());
        }

        //创建主张图表
        JFreeChart chart =  ChartFactory.createBarChart3D(
                "销售管理报表",//图形的主标题
                "月份",//种类轴标签
                "数量",//值轴的标签
                dataset, //图形的数据集合
                PlotOrientation.VERTICAL,//图形的显示样式(水平和垂直)
                true,//是否显示子标题
                true,//是否生产数据提示
                true);//是否生产url链接

        //处理主标题乱码

        chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
        //处理子标题乱码
        chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,15));
        //获取图表区域对象
        /*
         * 三种获取图标对象的方法
         *  1.debug
         *  2.输出（system.out.println()）
         *  3.api文档
         */

        CategoryPlot categoryPlot = chart.getCategoryPlot();

        //取得x轴的对象
        CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
        //取得y轴的对象
        ValueAxis valueAxis = categoryPlot.getRangeAxis();

        //解决x轴上的乱码问题
        categoryAxis.setTickLabelFont(new Font("宋体",Font.BOLD,15));
        //解决x轴外的乱码问题
        categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,15));

        //解决y轴上的乱码问题
        valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,15));
        //解决y轴外的乱码问题
        valueAxis.setLabelFont(new Font("宋体",Font.BOLD,15));

        //设置y轴上的刻度
        NumberAxis3D numberAxis3D = (NumberAxis3D) valueAxis;
        numberAxis3D.setAutoTickUnitSelection(false);//设置手动设置刻度
        //设置刻度从1开始
        NumberTickUnit unit  = new  NumberTickUnit(1);

        //设置新刻度
        numberAxis3D.setTickUnit(unit);

        //获取绘图区域对象
        BarRenderer3D barRenderer3D = (BarRenderer3D) categoryPlot.getRenderer();

        //让图形苗条些
        barRenderer3D.setMaximumBarWidth(0.06);

        //在图形上产生数字
        barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        barRenderer3D.setBaseItemLabelsVisible(true);
        barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15));
        //使用Rrame加载图形
        ChartPanel chartPanel = new ChartPanel(chart);
        backgroundPanel.add(chartPanel);
    }
}
